//
//  TestViewController.m
//  导航栏
//
//  Created by 小果 on 16/7/7.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "TestViewController.h"

@interface TestViewController ()
@property (nonatomic, weak) UISegmentedControl *segment;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BgColor;
    self.title = self.str;
    
    // 设置返回按钮
    [self setupLeftWithBtn];
    
    // 创建SegmentControl空间
    [self createSegmentControl];
}

#pragma mark - 设置导航栏左上角的按钮
-(void)setupLeftWithBtn{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [btn addTarget:self action:@selector(dismissVC) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(0, 0, 25, 25);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

#pragma mark - 返回上一控制器
-(void)dismissVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 创建分段控件
-(void)createSegmentControl{
    NSArray *array = @[@"2列",@"3列",@"4列",@"5列",@"6列"];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment = segment;
    CGFloat segmentW = 300;
    CGFloat segmentX = ([UIScreen mainScreen].bounds.size.width - segmentW)*0.5;
    segment.frame = CGRectMake(segmentX, 70, segmentW, 30);
    segment.backgroundColor = [UIColor redColor];
    segment.tintColor = [UIColor greenColor];
    [segment addTarget:self action:@selector(indexChange:) forControlEvents:UIControlEventValueChanged];
    segment.selectedSegmentIndex = 0;
    [self.view addSubview:segment];
    [self adjustImagePosWithColumns:2 add:YES];

}

#pragma mark - 监听分段控件的值的改变
-(void)indexChange:(UISegmentedControl *)sender{
//    NSLog(@"%zd",sender.selectedSegmentIndex);
    
    // 添加动画
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.8];
    
    // 1、定义列数、行数
    // 每行的列数
    CGFloat columns = sender.selectedSegmentIndex + 2;
    [self adjustImagePosWithColumns:columns add:NO];
    
    // 结束动画
    [UIView commitAnimations];

}

#pragma mark 调整图片的位置
-(void)adjustImagePosWithColumns:(int)columns add:(BOOL)add
{
    // 1、定义列数、间距
    // 每个表情之间的间距 = （控制器View的宽度 - 列数 * 表情的宽度）/ （列数 + 1）
    CGFloat margin = (self.view.frame.size.width - columns * kImgWh) / (columns + 1);
    
    // 2、定义第一个表情的位置
    // 第一个表情的X值
    CGFloat oneX = margin;
    // 第一个表情的Y值
    CGFloat oneY = CGRectGetMaxY(self.segment.frame) + 20;
    
    // 3、创建所有的表情
    for (int i = 0; i < kInitCount; i++) {
        // i这个位置对应的列数
        int col = i % columns;
        // i这个位置对应的行数
        int row = i / columns;
        
        // 列数决定了X
        CGFloat x = oneX + col * (margin + kImgWh);
        // 行数决定了Y
        CGFloat y = oneY + row * (margin + kImgWh);
        
        if (add){ // 添加新的imageview
            int no = i % 9;
            // 拼接每个表情的名字
            NSString *imageName = [NSString stringWithFormat:@"01%d.png",no];
            
            [self addImg:imageName x:x y:y];
            
        }else {
            // 取出旧的imageview 设置x,y
            // 取出i + 1位置对应的imageview， 设置x,y 的值
            // + 1是为了跳过最前面的UISegmentControl
            UIView *child = self.view.subviews[i + 1];
            // 取出frame
            CGRect tempF = child.frame;
            // 修改x,y
            tempF.origin = CGPointMake(x, y);
            // 重新赋值
            child.frame = tempF;
        }
    }
}

#pragma mark 添加表情，icon表情名
-(void)addImg:(NSString *)icon x:(CGFloat)x y:(CGFloat)y
{
    UIImageView *one = [[UIImageView alloc] init];
    one.image = [UIImage imageNamed:icon];
    one.frame = CGRectMake(x, y, 40, 40);
    
    [self.view addSubview:one];
}

@end
